﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Antlr.Runtime;

namespace FixtureHighlighter
{
    public class Error : IComparable<Error>
    {
        internal const int minErr = 50;
        internal const int minWrn = 100;

        public string message;
        public int line;
        public int column;
        public int length;
        public bool isWarn;

        internal Error(string message, int line, int col, int len, bool warningOnly)
        {
            this.message = message;
            this.line = line;
            this.column = col;
            this.length = len;
            this.isWarn = warningOnly;
        }

        public int CompareTo(Error other)
        {
            if (line < other.line)
                return -1;
            else if (line > other.line)
                return 1;
            else if (column < other.column)
                return -1;
            else if (column > other.column)
                return 1;
            else
                return 0;
        }

        public bool Equals(Error other)
        {
            return (line == other.line && column == other.column);
        }

        public void Report()
        {
            Console.WriteLine(string.Format("Line {0}, column {1}  : {2}", line, column, message));
        }
    }

    public interface IErrorHandler
    {
        int ErrNum
        {
            get;
        }
        int WrnNum
        {
            get;
        }

        void AddError(string message, int line, int col, int len, int severity);
    }

    public class FixtureErrorHandler : IErrorHandler
    {
        const int errLev = 2;

        List<Error> _errors;
        int _errNum;
        int _wrnNum;

        public bool Errors
        {
            get
            {
                return _errNum > 0;
            }
        }
        public bool Warnings
        {
            get
            {
                return _wrnNum > 0;
            }
        }
        public int ErrNum
        {
            get
            {
                return _errNum;
            }
        }
        public int WrnNum
        {
            get
            {
                return _wrnNum;
            }
        }

        public FixtureErrorHandler()
        {
            _errors = new List<Error>();
        }

        public List<Error> SortedErrorList()
        {
            if (_errors.Count > 0)
            {
                _errors.Sort();
                return _errors;
            }
            else
            {
                return null;
            }
        }

        public void AddError(string message, IToken location, int severity)
        {
        }
        public void AddError(string message, int line, int col, int len, int severity)
        {
            bool warnOnly = severity < errLev;
            _errors.Add(new Error(message, line, col, len, warnOnly));
            if (warnOnly)
                _wrnNum++;
            else
                _errNum++;
        }
        public void AddError(string message, int line, int col, int len)
        {
            _errors.Add(new Error(message, line, col, len, false));
            _errNum++;
        }
        public void AddWarning(string message, int line, int col, int len)
        {
            _errors.Add(new Error(message, line, col, len, true));
            _wrnNum++;
        }
    }
}
